package br.com.fogas.utils.dialog;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;

public class ProgressDialogBuilder {
	
	private Context context;
	
	private boolean cancelable = false;

	private boolean indeterminate = true;

	private String title;

	private String message;
	
	private ProgressDialog dialog;

	private Handler handler;
	
	public ProgressDialogBuilder(Context context) {
		this.context = context;
	}
	
	public Context getContext() {
		return context;
	}

	public ProgressDialogBuilder setCancelable(boolean cancelable) {
		this.cancelable = cancelable;
		return this;
	}

	public ProgressDialogBuilder setIndeterminate(boolean indeterminate) {
		this.indeterminate = indeterminate;
		return this;
	}

	public ProgressDialogBuilder setTitle(String title) {
		this.title = title;
		return this;
	}

	public ProgressDialogBuilder setMessage(String message) {
		this.message = message;
		return this;
	}
	
	public ProgressDialog getDialog() {
		if(dialog == null) {
			dialog = ProgressDialog.show(context, title, message, indeterminate, cancelable);
		}
		return dialog;
	}
	
	public void showDialog() {
		getDialog();
	}
	
	public void closeDialog() {
		getDialog().dismiss();
	}
	
	public ProgressDialogBuilder setHandler(Handler handler) {
		this.handler = handler;
		return this;
	}
	
	public void updateTitle(final String title) {
		handler.post(new Runnable() {
			public void run() {
				dialog.setTitle(title);
			}
		});
	}
	
	public void updateMessage(final String message) {
		handler.post(new Runnable() {
			public void run() {
				dialog.setMessage(message);
			}
		});
	}
	
	public void updateDialog(final String title, final String message) {
		handler.post(new Runnable() {
			public void run() {
				dialog.setTitle(title);
				dialog.setMessage(message);
			}
		});
	}
}